
package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.QTeamGroup;
import com.neusoft.neuiotms.dcom.model.TeamGroup;
import com.neusoft.neuiotms.dcom.repository.CustomizedTeamGroupRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedTeamGroupRepositoryImpl extends AbstractRepository implements CustomizedTeamGroupRepository {

    @Override
    public List<TeamGroup> index(final Searchable searchable) {
        final QTeamGroup qTeamGroup = QTeamGroup.teamGroup;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<TeamGroup> query = this.query().selectFrom(qTeamGroup);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return TeamGroup.class;
    }

    @Override
    public Page<TeamGroup> search(final Pageable pageable, final Searchable searchable) {
        final QTeamGroup qTeamGroup = QTeamGroup.teamGroup;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<TeamGroup> query = this.query().selectFrom(qTeamGroup);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QTeamGroup qTeamGroup = QTeamGroup.teamGroup;

        // 名称检索
        where.and(super.containsStr(qTeamGroup.name, searchable, "name"));
        // 组织机构检索
        where.and(super.equalsStr(qTeamGroup.orgCode, searchable, "orgCode"));
        // in组织机构
        where.and(super.inStrings(qTeamGroup.orgCode, searchable, "orgCodes"));
        // 职能ID
        where.and(super.equalsLong(qTeamGroup.functionalGroup.id, searchable, "functionalGroupId"));
        // 人员查询
        where.and(super.equalsStr(qTeamGroup.users.any().code, searchable, "userName"));
        return where;
    }
}
